local sk__wangyued = fk.CreateSkill {

  name = "sk__wangyued",

  tags = {},

}



sk__wangyued:addEffect(fk.AfterCardsMove, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(sk__wangyued.name) then
      local room = player.room
      if event == fk.AfterCardsMove and player:getMark("sk__wangyued1-turn") == 0 then
        for _, move in ipairs(data) do
          if move.moveReason == fk.ReasonDiscard and move.from then
            event:setCostData(self, { 1, move.from, #move.moveInfo })
            return table.find(room:getOtherPlayers(move.from), function(p)
              return not p.dead
            end)
          end
        end
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = room:getOtherPlayers(event:getCostData(self)[2])
    if event:getCostData(self)[1] == 2 then
      targets = table.filter(targets, function(p)
        return p:isWounded()
      end)
    end
    local to = room:askForChoosePlayers(player, table.map(targets, Util.IdMapper), 1, 1,
      "#sk__wangyued" .. event:getCostData(self)[1] .. "-invoke:::" .. event:getCostData(self)[3], sk__wangyued.name, true)
    if #to > 0 then
      event:setCostData(self, { event:getCostData(self)[1], to[1], event:getCostData(self)[3] })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "sk__wangyued" .. event:getCostData(self)[1] .. "-turn", 1)
    local to = room:getPlayerById(event:getCostData(self)[2])
    if event:getCostData(self)[1] == 1 then
      to:drawCards(event:getCostData(self)[3], sk__wangyued.name)
    elseif event:getCostData(self)[1] == 2 then
      room:recover({
        who = to,
        num = math.min(event:getCostData(self)[3], to:getLostHp()),
        recoverBy = player,
        skillName = sk__wangyued.name,
      })
    elseif event:getCostData(self)[1] == 3 then
      room:changeMaxHp(to, event:getCostData(self)[3])
    end
  end,
})
sk__wangyued:addEffect(fk.HpLost, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(sk__wangyued.name) then
      local room = player.room
      if event == fk.HpLost and player:getMark("sk__wangyued2-turn") == 0 then
        if table.find(room:getOtherPlayers(target), function(p)
              return p:isWounded()
            end) then
          event:setCostData(self, { 2, target.id, data.num })
          return true
        end

      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = room:getOtherPlayers(room:getPlayerById(event:getCostData(self)[2]))
    if event:getCostData(self)[1] == 2 then
      targets = table.filter(targets, function(p)
        return p:isWounded()
      end)
    end
    local to = room:askForChoosePlayers(player, table.map(targets, Util.IdMapper), 1, 1,
      "#sk__wangyued" .. event:getCostData(self)[1] .. "-invoke:::" .. event:getCostData(self)[3], sk__wangyued.name, true)
    if #to > 0 then
      local cost_data=event:getCostData(self)
      cost_data[2]=to[1]
      event:setCostData(self, cost_data)
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "sk__wangyued" .. event:getCostData(self)[1] .. "-turn", 1)
    local to = room:getPlayerById(event:getCostData(self)[2])
    if event:getCostData(self)[1] == 1 then
      to:drawCards(event:getCostData(self)[3], sk__wangyued.name)
    elseif event:getCostData(self)[1] == 2 then
      room:recover({
        who = to,
        num = math.min(event:getCostData(self)[3], to:getLostHp()),
        recoverBy = player,
        skillName = sk__wangyued.name,
      })
    elseif event:getCostData(self)[1] == 3 then
      room:changeMaxHp(to, event:getCostData(self)[3])
    end
  end,
})
sk__wangyued:addEffect(fk.MaxHpChanged, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(sk__wangyued.name) then
      local room = player.room
      if event == fk.MaxHpChanged and player:getMark("sk__wangyued3-turn") == 0 then
        if data.num < 0 then
          event:setCostData(self, { 3, target.id, -data.num })
          return table.find(room:getOtherPlayers(target), function(p)
            return not p.dead
          end)
        end
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = room:getOtherPlayers(room:getPlayerById(event:getCostData(self)[2]))
    if event:getCostData(self)[1] == 2 then
      targets = table.filter(targets, function(p)
        return p:isWounded()
      end)
    end
    local to = room:askForChoosePlayers(player, table.map(targets, Util.IdMapper), 1, 1,
      "#sk__wangyued" .. event:getCostData(self)[1] .. "-invoke:::" .. event:getCostData(self)[3], sk__wangyued.name, true)
    if #to > 0 then
      event:getCostData(self)[2] = to[1]
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "sk__wangyued" .. event:getCostData(self)[1] .. "-turn", 1)
    local to = room:getPlayerById(event:getCostData(self)[2])
    if event:getCostData(self)[1] == 1 then
      to:drawCards(event:getCostData(self)[3], sk__wangyued.name)
    elseif event:getCostData(self)[1] == 2 then
      room:recover({
        who = to,
        num = math.min(event:getCostData(self)[3], to:getLostHp()),
        recoverBy = player,
        skillName = sk__wangyued.name,
      })
    elseif event:getCostData(self)[1] == 3 then
      room:changeMaxHp(to, event:getCostData(self)[3])
    end
  end,
})

return sk__wangyued
